<?php
class SimpleChangeTrackerListener extends Doctrine_Record_Listener {
	
	private static $format = 'Y-m-d H:i:s';
	
	public function preInsert(Doctrine_Event $event) {
		$invoker = $event->getInvoker();
		$id = $this->getUserId($event);
		$invoker->created_by = $id;
		$invoker->created_at = date(self::$format);
	}
	
	public function preUpdate(Doctrine_Event $event) {
		$invoker = $event->getInvoker();
		$invoker->updated_by = $this->getUserId($event);
		$invoker->updated_at = date(self::$format);
	}
	
	public function preDelete(Doctrine_Event $event) {
		$invoker = $event->getInvoker();
		$invoker->deleted_by = $this->getUserId($event);
		$invoker->deleted_at = date(self::$format);
	}
	
	private function getUserId(Doctrine_Event $event) {
		$invoker = $event->getInvoker();
		$id = null;
		try {
			$id = (int)sfContext::getInstance()->getUser()->getGuardUser()->getId();
		} catch(Exception $ex) { }
		return $id;
	}
}

?>